package com.supms.course.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.supms.course.domain.Attend;
import com.supms.course.domain.AttendEval;
import com.supms.course.mapper.AttendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.AttendItemsMapper;
import com.supms.course.domain.AttendItems;
import com.supms.course.service.IAttendItemsService;

/**
 * 听课评价项Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-02-27
 */
@Service
public class AttendItemsServiceImpl implements IAttendItemsService 
{
    @Autowired
    private AttendItemsMapper attendItemsMapper;

    @Autowired
    private AttendMapper attendMapper;

    /**
     * 查询听课评价项
     * 
     * @param id 听课评价项主键
     * @return 听课评价项
     */
    @Override
    public AttendItems selectAttendItemsById(Long id)
    {
        return attendItemsMapper.selectAttendItemsById(id);
    }

    /**
     * 查询听课评价项列表
     * 
     * @param attendItems 听课评价项
     * @return 听课评价项
     */
    @Override
    public List<AttendItems> selectAttendItemsList(AttendItems attendItems)
    {
        return attendItemsMapper.selectAttendItemsList(attendItems);
    }

    /**
     * 新增听课评价项
     * 
     * @param attendItems 听课评价项
     * @return 结果
     */
    @Override
    public int insertAttendItems(AttendItems attendItems)
    {
        attendItems.setCreateTime(DateUtils.getNowDate());
        return attendItemsMapper.insertAttendItems(attendItems);
    }

    /**
     * 修改听课评价项
     * 
     * @param attendItems 听课评价项
     * @return 结果
     */
    @Override
    public int updateAttendItems(AttendItems attendItems)
    {
        attendItems.setUpdateTime(DateUtils.getNowDate());
        return attendItemsMapper.updateAttendItems(attendItems);
    }

    /**
     * 批量删除听课评价项
     * 
     * @param ids 需要删除的听课评价项主键
     * @return 结果
     */
    @Override
    public int deleteAttendItemsByIds(Long[] ids)
    {
        return attendItemsMapper.deleteAttendItemsByIds(ids);
    }

    /**
     * 删除听课评价项信息
     * 
     * @param id 听课评价项主键
     * @return 结果
     */
    @Override
    public int deleteAttendItemsById(Long id)
    {
        return attendItemsMapper.deleteAttendItemsById(id);
    }

    /**
     * 获取当前课类型的听课评价表
     *
     * @param typeNum 课程类型标志
     * @return 结果
     */
    @Override
    public List<AttendItems> selectAttendItemByType(String typeNum) {
        return attendItemsMapper.selectAttendItemByType(typeNum);
    }

    /**
     * 添加听课评价
     * @param attendItemsList 接收到的评分列表
     * @return 结果
     */
    @Override
    public int addAttenEval(List<AttendItems> attendItemsList) {

        List<AttendEval> attendEvalList = new ArrayList<>();
        Integer totalScore = 0;
        Long attendId = null;

        for (AttendItems attendItem : attendItemsList) {

            // 判断是否未填分数就提交，不为空则进行下一步流程
            Integer point = attendItem.getPoint();
            if (point != null) {
                totalScore = totalScore + point;
                attendId = attendItem.getAttendId();

                AttendEval eval = new AttendEval();
                eval.setAttendId(attendId);
                eval.setAtteItemId(attendItem.getId());
                eval.setPoint(point);
                eval.setRemarks(attendItem.getRemarks());
                eval.setCreateTime(attendItem.getCreateTime());
                attendEvalList.add(eval);
            } else return 0;
        }

        Attend attend = new Attend();
        if (totalScore >= 90) {
            attend.setTotalScore(totalScore);
            attend.setLevel("A");
            attend.setId(attendId);
            attendMapper.inputTotalScoreAndLevelById(attend);
        }
        if (totalScore < 90 && totalScore >= 80) {
            attend.setTotalScore(totalScore);
            attend.setLevel("B");
            attend.setId(attendId);
            attendMapper.inputTotalScoreAndLevelById(attend);
        }
        if (totalScore < 80 && totalScore >= 60) {
            attend.setTotalScore(totalScore);
            attend.setLevel("C");
            attend.setId(attendId);
            attendMapper.inputTotalScoreAndLevelById(attend);
        }
        if (totalScore < 60 && totalScore >= 0) {
            attend.setTotalScore(totalScore);
            attend.setLevel("D");
            attend.setId(attendId);
            attendMapper.inputTotalScoreAndLevelById(attend);
        }
        return attendItemsMapper.addAttenEval(attendEvalList);
    }

    /**
     * 通过听课表ID查看听课评价
     * @param attendId 听课表ID
     * @return 结果
     */
    @Override
    public List<AttendItems> getAttendEvalByAttendId(Long attendId) {
        return attendItemsMapper.getAttendEvalByAttendId(attendId);
    }
}
